﻿using System;
using System.Collections.Generic;
using System.Linq;
using TweetSharp;

namespace Microsoft.Coding4Fun.TweetCraft.Twitter
{
    /// <summary>
    /// Contains extension methods to work with <see cref="TwitterStatus"/> and <see cref="TwitterUser"/> 
    /// objects returned by Tweet#.
    /// </summary>
    internal static class TweetSharpExtensions
    {
        public static IEnumerable<Tweet> ToTweets(this IEnumerable<TwitterStatus> twitterStatuses)
        {
            if (twitterStatuses == null)
                throw new ArgumentNullException("twitterStatuses");

            return twitterStatuses.Select(twitterStatus => twitterStatus.ToTweet());
        }

        public static Tweet ToTweet(this TwitterStatus twitterStatus)
        {
            if (twitterStatus == null)
            {
                return null;
            }

            return new Tweet
                   {
                       Id = twitterStatus.Id,
                       DateSent = twitterStatus.CreatedDate,
                       Message = twitterStatus.Text,
                       User = twitterStatus.User.ToUser()
                   };
        }

        private static User ToUser(this TwitterUser user)
        {
            if (user == null)
                return null;

            return new User
                   {
                       Id = user.Id,
                       ScreenName = user.ScreenName,
                       ImageUrl = user.ProfileImageUrl.ToUri()
                   };
        }
    }
}